<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav"
                    @itemClick="navItemClick" ref="navBar"/>
    <scroll class="center"
            ref="scroll"
            :probe-type="3"
            @scroll="themeScroll">
      <detail-swiper :top-images="topImages"/>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shop"/>
      <detail-goods-info :detail-info="detailInfo"
                         @imgLoad="imgLoad"/>
      <detail-param-info :param-info="paramInfo"
                         ref="param"/>
      <detail-comment-info :comment-info="commentInfo"
                           ref="comment"/>
      <br>
      <p align="center">更多推荐</p>
      <br>
      <goods-list :goods="recommends"
                  ref="recommend"/>
    </scroll>
    <back-top @click.native="backClick"
              v-show="isShowBackTop"/>
    <detail-bottom-bar @addCart="addToCart" :price="goods.realPrice"/>
  </div>
</template>

<script>

  import {getDetail,getRecommend,Goods,shop,Param} from "@/network/detail";
  import {itemImgLoad,backTopMixin} from "@/common/mixin";
  import {debounce} from "@/common/utils";
  import {mapActions} from 'vuex'
  import Scroll from "@/components/common/scroll/Scroll";

  import DetailNavBar from "@/views/detail/childComps/DetailNavBar";
  import DetailBaseInfo from "@/views/detail/childComps/DetailBaseInfo";
  import DetailSwiper from "@/views/detail/childComps/DetailSwiper";
  import DetailShopInfo from "@/views/detail/childComps/DetailShopInfo";
  import DetailParamInfo from "@/views/detail/childComps/DetailParamInfo";
  import DetailGoodsInfo from "@/views/detail/childComps/DetailGoodsInfo";
  import DetailCommentInfo from "@/views/detail/childComps/DetailCommentInfo";
  import GoodsList from "@/components/content/goods/GoodsList";
  import DetailBottomBar from "@/views/detail/childComps/DetailBottomBar";

  export default {
    name: "Detail",

    components:{
      DetailNavBar,
      DetailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      DetailParamInfo,
      DetailGoodsInfo,
      DetailCommentInfo,
      Scroll,
      GoodsList,
      DetailBottomBar
    },
    data(){
      return{
        iid: null,
        topImages: [],
        goods: {},
        shop:{},
        detailInfo: {},
        paramInfo: {},
        commentInfo: [],
        recommends: [],
        themeTopYs: [],
        getThemeTopYs: null
      }
    },

    methods:{
      ...mapActions(['addCart']),
      imgLoad() {
        this.$refs.scroll.refresh()
        this.getThemeTopYs()
      },
      navItemClick(index) {
        this.$refs.scroll.scrollTo(0,-this.themeTopYs[index],100)
      },
      themeScroll(position) {

        this.listenShowBackTop(position)

        setTimeout(()=>{
          if(-position.y >= this.themeTopYs[3]){
            this.$refs.navBar.currentIndex = 3
          }else if(-position.y >= this.themeTopYs[2]){
            this.$refs.navBar.currentIndex = 2
          }else if(-position.y >= this.themeTopYs[1]){
            this.$refs.navBar.currentIndex = 1
          }else{
            this.$refs.navBar.currentIndex = 0
          }
        },101)
      },
      addToCart() {
        const product = {}
        product.img = this.topImages[0]
        product.title = this.goods.title
        product.desc = this.goods.desc
        product.price = this.goods.realPrice
        product.iid = this.iid
        product.checked = false
        this.addCart(product).then(res => {
          this.$toast.show(res,1500)
        })
      }
    },
    mixins:[itemImgLoad,backTopMixin],
    created() {
      this.iid = this.$route.params.iid

      this.getThemeTopYs = debounce(() => {
        this.themeTopYs = []
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.param.$el.offsetTop)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
      },100)

      getDetail(this.iid).then(res => {
        const data = res.result
        console.log(res);
        this.topImages = data.itemInfo.topImages

        this.goods = new Goods(data.itemInfo,data.columns,data.shopInfo.services)

        this.shop = new shop(data.shopInfo);

        this.detailInfo = data.detailInfo;

        this.paramInfo = new Param(data.itemParams.info, data.itemParams.rule)
        if(data.rate.cRate !== 0){
          this.commentInfo = data.rate.list
        }
      })
      getRecommend().then(res => {
        this.recommends = res.data.list
      })
    }
  }
</script>

<style scoped>
  #detail {
    position: relative;
    z-index: 11;
    background-color: #fff;
    height: 100vh;
  }
  .detail-nav {
    position: relative;
    z-index: 3;
  }
  .center {
    height: calc(100% - 44px - 49px);
  }
</style>
